<?php

// 连接 Redis 服务
try {
    $redis = new Redis();
    $redis->connect('172.18.0.5', 6379);
    echo "<div>Connection to server <span style='color: green;'>successfully</sapn></div>" . PHP_EOL; // PHP_EOL只是用于终端输出换行, <br/>用于浏览器换行

    // 检测服务运行状态
    if ($redis->ping()) {
        $color = 'green';
        $state = 'running';
    } else {
        $color = 'red';
        $state = 'stop';
    }
    echo "Server state: <span style=\"color: {$color}\">{$state}</span> <br/>" . PHP_EOL;

    // 设置 redis 字符串数据
    $redis->set("words", "hello redis~");
    // 获取存储的数据并输出
    echo "Stored string in redis:: <span style='color: green;'>" . $redis->get("words") . '</span> <br/>' . PHP_EOL;
} catch (Exception $e) {
    die("Error!: {$e->getMessage()} <br/>");
}
